import { _decorator, Component, Node, tween, v3, Label } from 'cc';
import { Card } from './Card';
import GameAllData from './GameAllData';
import { SoltaireGame } from './SoltaireGame';
import { Dictionary } from './tool/Dictionary';
import { SoundEffect } from './tool/SoundEffect';
import * as i18n from 'db://i18n/LanguageData';

const { ccclass, property } = _decorator;

@ccclass('CardGroupParent')
export class CardGroupParent extends Component {
    @property(Number)
    groupId: number= 0;//当前组id

    @property(Node)
    cardsNode: Node= null!;

    @property(Node)
    info: Node= null!;

    @property(Node)
    scoreNode: Node= null!;

    @property(Node)
    tipsNode: Node= null!;

    cards: Card[] = [];

    cardIsShow=false;

    infoSelect:Node;
    scoreLabel:Label;
    tipsLabel:Label;

    cardIsOver=false;
    onLoad()
    {
        this.infoSelect=this.info.getChildByName("select");
        this.scoreLabel=this.scoreNode.getChildByName("text").getComponent(Label);
        this.tipsLabel=this.tipsNode.getChildByName("text").getComponent(Label);

        this.scoreNode.active=false;
        this.tipsNode.active=false;
    }

    AddOneCard(card)
    {
        this.cards.push(card);
    }

    

    //获取最终节点
    GetLastCard()
    {
        if (this.cards.length>0) {
            return this.cards[this.cards.length-1];
        }

        return null;
    }

    GetCardNumber()
    {
        return this.cards.length;
    }

    //选择弃牌
    SelectPass()
    {
        this.cardIsOver=true;

        for (let i = 0; i < this.cards.length; i++) {
            const card = this.cards[i];
            tween(card.node).delay((4-i)*0.05).to(0.5, {worldPosition:SoltaireGame.instance.dealBtn.worldPosition},{easing:"cubicOut"})
            .start()
        }
        this.scoreNode.active=false;
        this.tipsNode.active=true;
        this.ShowTipsLabel(i18n.t("button1"));
        SoundEffect.instance.playOneShotName("pass");
    }

    curScore=0;
    //选择跟分
    SelectGen(sc)
    {
        this.scoreNode.active=true;
        this.tipsNode.active=true;
        this.ShowScoreLabel(sc,sc-this.curScore);
        this.ShowTipsLabel(i18n.t("button2")+":"+(sc-this.curScore));
        if (this.groupId==0) {
            SoltaireGame.instance.AddMenoy(-(sc-this.curScore));
        }
        this.curScore=sc;
        GameAllData.Instance.lastPlayUpScore=sc;

        SoundEffect.instance.playOneShotName("dahai");
    }

    //选择加分
    SelectAdd100(sc)
    {
        this.scoreNode.active=true;
        this.tipsNode.active=true;
        this.ShowScoreLabel(sc,sc-this.curScore);
        this.ShowTipsLabel(i18n.t("button3")+":"+(sc-this.curScore));
        if (this.groupId==0) {
            SoltaireGame.instance.AddMenoy(-(sc-this.curScore));
        }
        this.curScore=sc;
        GameAllData.Instance.lastPlayUpScore=sc;
        SoundEffect.instance.playOneShotName("spades_nil");
    }

    //出的牌组
    selectCards:Card[]=[];

    //手动出的牌组
    SelectCards(card:Card,isadd:boolean)
    {
        if (isadd) {
            this.selectCards.push(card);
        } else {
            for (let i = 0; i < this.selectCards.length; i++) {
                const element = this.selectCards[i];
                if (element==card) {
                    this.selectCards.splice(i,1);
                    break;
                }
            }
        }
    }

    //选择交换
    SelectInterchangeCard()
    {
        for (let i = 0; i < this.selectCards.length; i++) {
            const card = this.selectCards[i];
            card.QuxiaoXuanZhong();
            let startPos=card.node.worldPosition.clone();
            tween(card.node).delay(i*0.1)
            .to(0.5, {worldPosition:SoltaireGame.instance.dealBtn.worldPosition},{easing:"cubicOut"})
            .call(()=>{//交换卡牌
                card.Init(SoltaireGame.instance.RetrueRandomCard(),this);
                SoundEffect.instance.playOneShotName("card_send");
            })
            .to(0.5, {worldPosition:startPos},{easing:"cubicOut"})
            .start()
            SoundEffect.instance.playOneShotName("card_send");
        }
    }

    ShowTipsLabel(str)
    {
        this.tipsLabel.string=str;
    }

    ShowScoreLabel(num,add)
    {
        this.scoreLabel.string=num.toString();
        SoltaireGame.instance.UpdataAllScore(add);
    }

    //获取排序后卡组
    GetSortCards()
    {
        let cardClone:Card[]=[];
        for (let i = 0; i < this.cards.length; i++) {
            const element = this.cards[i];
            cardClone.push(element);
        }
        cardClone.sort((a,b)=>{
            if (a.cardData==501) {
                return 1;
            } else 
            {
                if (a.cardNumber>b.cardNumber) {
                    return 1;
                } else if (a.cardNumber==b.cardNumber){
                    if (a.cardColor>b.cardColor) {
                        return 1;
                    } else {
                        return -1;
                    }
                }else
                {
                    return -1;
                }
            }
        })

        return cardClone;
    }

    //获取牌型
    GetPokerHands():number
    {
        let cardClone:Card[]=this.GetSortCards();

        let haveStraigh=true;//有顺子
        for (let i = 1; i < 5; i++) {
            const card = cardClone[i];
            if (card.cardNumber!=(cardClone[0].cardNumber+i)) 
            {
                haveStraigh=false;
                break;
            }
        }

        let haveSameColor=false;//有童话
        let sameColor=[0,0,0,0];//花色
        for (let i = 0; i < 5; i++) {
            const card = cardClone[i];
            sameColor[card.cardColor-1]++;
        }

        for (let i = 0; i < 4; i++) {
            if (sameColor[i]==5) {
                haveSameColor=true;
                break;
            }
        }

        if (haveStraigh&&haveSameColor) 
        {//同花顺
            return 1;
        }

        let cardNum=[0,0,0,0,0,0,0,0,0,0,0,0,0]; 
        for (let i = 0; i < 5; i++) {
            const card = cardClone[i];
            cardNum[card.cardNumber-1]++;
        }

        for (let i = 0; i < cardNum.length; i++) {
            if (cardNum[i]==4) 
            {//四张
                return 2;
            }
        }

        let isTwo=false;
        let isThree=false;
        for (let i = 0; i < cardNum.length; i++) {
            if (cardNum[i]==2) {
                isTwo=true;
            }
            if (cardNum[i]==3) {
                isThree=true;
            }
        }

        if (isTwo&&isThree) 
        {//葫芦
            return 3;
        }

        if (haveSameColor) 
        {//同花
            return 4;
        }
        
        if (haveStraigh) 
        {//顺子
            return 5;
        }

        let pairs=0;
        let Sanjo=0;
        for (let i = 0; i < cardNum.length; i++) {
            if (cardNum[i]==2) {
                pairs++;
            }
            if (cardNum[i]==3) {
                Sanjo++;
            }
        }

        if (Sanjo==1) {
            return 6;
        }

        if (pairs==2) 
        {//两队
            return 7;
        }else if (pairs==1)
        {//一对
            return 8;
        }

        return 9;//杂牌
    }


    //排序
    SortCards()
    {
        this.cards.sort((a,b)=>{
            if (a.cardData==501) {
                return 1;
            } else 
            {
                if (b.cardData==501) {
                    return -1;
                }

                if (a.cardNumber>b.cardNumber) {
                    return 1;
                } else if (a.cardNumber==b.cardNumber){
                    if (a.cardColor>b.cardColor) {
                        return 1;
                    } else {
                        return -1;
                    }
                }else
                {
                    return -1;
                }
            }
        })
    }

    //位置排序
    PositionSort()
    {
  
    }

    foreCards=[];
    //强制选择最强卡牌
    ForceSelect(num:number)
    {
        if (num==2) {
            this.foreCards.push(this.cards[this.cards.length-1]);
            //this.cards[this.cards.length-1].InterchangeSelect();
            this.foreCards.push(this.cards[this.cards.length-2]);
            //this.cards[this.cards.length-2].InterchangeSelect();
        }else 
        {
            this.foreCards.push(this.cards[this.cards.length-1]);
            //this.cards[this.cards.length-1].InterchangeSelect();
        } 
    }

    interchangeCards=[];
    //智能选择
    AiInterchangeSelect(num:number)
    {
        
    }

    // 设置人物选中信息
    SetInfoSelet(_is:boolean)
    {
        this.infoSelect.active=_is;
    }

    SetInfo(lv,score)
    {
        this.scoreLabel.string=score;
    }

    /**
     * 
     * @param cards 
     * @returns 1:单张 2：同张 3：顺子 
     */
    PanduanLeixing(cards:Card[])
    {
        if (cards.length==1) {
            return 1;
        }
        if (cards.length==2) {
            return 2;
        }

        let start=cards[0];
        let start2=cards[1];

        if (start.cardNumber==start2.cardNumber) {
            return 2;
        }else
        {
            return 3;
        }
    }


    //ai选择出分
    AiSelectChuScore()
    {

    }
    //ai选择出牌
    AiSelectCards()
    {

    }

    //卡牌类型
    CardsIsType()
    {



    }


    //清空牌组
    QingkongCards()
    {
        for (let i = 0; i < this.selectCards.length; i++) {
            const seltct = this.selectCards[i];
            for (let j = 0; j < this.cards.length; j++) {
                const cd = this.cards[j];
                if (seltct==cd) {
                    this.cards.splice(j,1);
                    continue;
                }
            }
        }

        this.PositionSort();
    }

    //判断自由出 是否合理
    CardIsConformToNull(selectCDs:Card[])
    {
        if (selectCDs.length==0) {
            return;
        }

        selectCDs.sort((a,b)=>{
            if (a.cardData==501) {
                return 1;
            } else 
            {
                if (b.cardData==501) {
                    return -1;
                }

                if (a.cardNumber>b.cardNumber) {
                    return 1;
                } else if (a.cardNumber==b.cardNumber){
                    if (a.cardColor>b.cardColor) {
                        return 1;
                    } else {
                        return -1;
                    }
                }else
                {
                    return -1;
                }
            }
        })

        //单张
        if (selectCDs.length==1) {
            return true;
        }

        let isWanNeng=false;
        //万能牌
        if (selectCDs[selectCDs.length-1].cardData==501) {
            isWanNeng=true;
        }

        //对子
        if(selectCDs.length==2)
        {
            if (isWanNeng) {
                return true;
            } else {
                if (selectCDs[0].cardNumber==selectCDs[1].cardNumber) {
                    return true;
                }
            }
        }

        //
        if (selectCDs.length>=3) {
            //三对
            if (isWanNeng) {
                if (selectCDs[0].cardNumber==selectCDs[1].cardNumber) {
                    return true;
                }
            } else {
                if (selectCDs[0].cardNumber==selectCDs[1].cardNumber&&selectCDs[0].cardNumber==selectCDs[2].cardNumber) {
                    return true;
                }
            }

            //四对
            if (isWanNeng) {
                if (selectCDs[0].cardNumber==selectCDs[1].cardNumber&&selectCDs[0].cardNumber==selectCDs[2].cardNumber) {
                    return true;
                }
            } else {
                if (selectCDs[0].cardNumber==selectCDs[1].cardNumber&&selectCDs[0].cardNumber==selectCDs[2].cardNumber&&selectCDs[0].cardNumber==selectCDs[3].cardNumber) {
                    return true;
                }
            }

            let curColor=selectCDs[0].cardColor;
            let curNum=selectCDs[0].cardNumber;
            if (isWanNeng) {
                let wanNeng=true;
                for (let i = 1; i < selectCDs.length-1; i++) {
                    const element = selectCDs[i];
                    if (element.cardColor!=curColor) {
                        return false;
                    }
                    if (curNum+1!=element.cardNumber) {
                        if (wanNeng) {
                            wanNeng=false;
                            curNum=curNum+1;
                            continue;
                        }
                        return false;
                    }
                    curNum=curNum+1;
                }
                return true;
            } else {
                for (let i = 1; i < selectCDs.length; i++) {
                    const element = selectCDs[i];
                    if (element.cardColor!=curColor) {
                        return false;
                    }
                    if (curNum+1!=element.cardNumber) {
                        return false;
                    }
                    curNum=curNum+1;
                }
                return true;
            }

        }

        return false;
    }


}


